package com.worldcup.service.relation.impl;

import com.worldcup.dao.relation.UserRelationshipEventMapper;
import com.worldcup.model.question.UserRelationshipEvent;
import com.worldcup.service.relation.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author duwan.zq
 * @date 2019/03/22
 */
@Service
public class UserRelationServiceImpl implements UserRelationService {

    @Autowired
    private UserRelationshipEventMapper mapper;

    @Override
    public void insert(UserRelationshipEvent relationshipEvent) {
        LocalDateTime now = LocalDateTime.now();
        relationshipEvent.setCreatedAt(now);
        relationshipEvent.setUpdatedAt(now);
        mapper.insert(relationshipEvent);
    }

    @Override
    public List<String> getFriendIds(String openid) {
        List<UserRelationshipEvent> relationshipEvents = mapper.selectByOpenid(openid, 1);
        return relationshipEvents.stream().map(UserRelationshipEvent::getFriendOpenid).collect(Collectors.toList());
    }

    @Override
    public List<String> getOpenids(String openid) {
        List<UserRelationshipEvent> relationshipEvents = mapper.selectByFriendId(openid, 1);
        return relationshipEvents.stream().map(UserRelationshipEvent::getOpenid).collect(Collectors.toList());
    }
}
